<?php

declare(strict_types=1);

namespace app\admin\controller;

use app\admin\controller\Base;
use app\admin\service\CustomerInvoicebasicinfo as CustomerInvoicebasicinfoService;

use think\Request;

class CustomerInvoicebasicinfo extends Base
{

    protected $request;
    protected $service;
    protected $validate;
    protected $operator_id;

    public function __construct(Request $request,  CustomerInvoicebasicinfoService $service)
    {
        $this->request = $request;
        $this->service = $service;
        $this->validate = new \app\admin\validate\CustomerInvoicebasicinfo();
        $this->operator_id = $request->operator_id;
    }
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $params = empty($this->request->params) ? [] : $this->request->params;

        $pageIndex         = isset($params['pageIndex'])             ? intval($params['pageIndex'])             : 1;
        $pageSize          = isset($params['pageSize'])              ? intval($params['pageSize'])              : 1000;
        $customerId        = isset($params['customer_id']) ? intval($params['customer_id']) : null;
        $where = ['is_del' => 0]; // $where = [];
        if ($customerId > -1) {
            $where['customer_id'] = $customerId;
        }
        $field = '*';
        $res =  $this->service->getList($pageIndex, $pageSize, $where, $field);

        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }


    /**
     * 显示创建资源表单页.
     * @author: Bobo
     * @date: 2025-02-07 16:51:40 
     * @return \think\Response
     */
    public function create()
    {
        $params = $this->request->params;
        if (!$this->validate->scene('create')->check($params)) {
            return $this->returnJsonData(100001, null, $this->validate->getError());
        }

        $params['create_time'] = date('Y-m-d H:i:s', time());
        $params['creator'] = $this->operator_id;

        $res  = $this->service->create($params);
        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $params = $this->request->params;
        if (!$this->validate->scene('update')->check($params)) {
            return $this->returnJsonData(100001, [], $this->validate->getError());
        }
        $params['updater'] = $this->operator_id;
        $params['update_time'] = date('Y-m-d H:i:s', time());

        $where = [];
        if ($params['id']) {
            $where['id'] = intval($params['id']);
        }
        if ($params['customer_id']) {
            $where['customer_id'] = intval($params['customer_id']);
        }
        $res  = $this->service->update($where, $params);

        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete()
    {
        $params = $this->request->params;
        if (!$this->validate->scene('delete')->check($params)) {
            return $this->returnJsonData(100001, [], $this->validate->getError());
        }
        $ids =  trim($params['ids']);
        $res = $this->service->batchLogicalDelete($ids);
        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }
}
